package com.mission.schedule.my160920.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.aspsine.swipetoloadlayout.SwipeRefreshTrigger;
import com.aspsine.swipetoloadlayout.SwipeTrigger;
import com.mission.schedule.R;

/**
 * Created by liwei on 2016/10/27.
 */

public class OnRefreshRecyclerViewHeader extends LinearLayout implements SwipeTrigger,SwipeRefreshTrigger{

    private View mContainer;
    private ImageView mArrowImageView;
    private ProgressBar mProgressBar;
    private TextView mHintTextView;
    private int mState = STATE_NORMAL;

    private Animation mRotateUpAnim;
    private Animation mRotateDownAnim;

    private final int ROTATE_ANIM_DURATION = 180;

    public final static int STATE_NORMAL = 0;
    public final static int STATE_READY = 1;
    public final static int STATE_REFRESHING = 2;

    public OnRefreshRecyclerViewHeader(Context context) {
        super(context);
        initView(context);
    }

    public OnRefreshRecyclerViewHeader(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public OnRefreshRecyclerViewHeader(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }


    private void initView(Context context) {
        LayoutParams lp = new LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        mContainer = LayoutInflater.from(context).inflate(
                R.layout.xlistview_header, null);
        addView(mContainer, lp);

        mArrowImageView = (ImageView) mContainer.findViewById(R.id.xlistview_header_arrow);
        mHintTextView = (TextView) mContainer.findViewById(R.id.xlistview_header_hint_textview);
        mProgressBar = (ProgressBar) mContainer.findViewById(R.id.xlistview_header_progressbar);
//        mProgressBar.setVisibility(View.GONE);
        mRotateUpAnim = new RotateAnimation(0.0f, -180.0f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                0.5f);
        mRotateUpAnim.setDuration(ROTATE_ANIM_DURATION);
        mRotateUpAnim.setFillAfter(true);
        mRotateDownAnim = new RotateAnimation(-180.0f, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                0.5f);
        mRotateDownAnim.setDuration(ROTATE_ANIM_DURATION);
        mRotateDownAnim.setFillAfter(true);
    }

    @Override
    public void onRefresh() {
//        mProgressBar.setVisibility(View.VISIBLE);
//        mArrowImageView.clearAnimation();
//        mArrowImageView.setVisibility(View.GONE);
        mHintTextView.setText(R.string.xlistview_header_hint_loading);
    }

    @Override
    public void onPrepare() {
        mHintTextView.setText(R.string.xlistview_header_hint_normal);
    }

    @Override
    public void onSwipe(int i, boolean b) {
//        mProgressBar.setVisibility(View.GONE);
//        mArrowImageView.setVisibility(View.VISIBLE);
//        mArrowImageView.clearAnimation();
//        mArrowImageView.startAnimation(mRotateUpAnim);
        mHintTextView.setText(R.string.xlistview_header_hint_ready);
    }

    @Override
    public void onRelease() {
        mHintTextView.setText(R.string.xlistview_header_hint_ready);
    }

    @Override
    public void complete() {
//        mArrowImageView.setVisibility(View.GONE);
//        mProgressBar.setVisibility(View.GONE);
        mHintTextView.setText("加载完成");
    }

    @Override
    public void onReset() {

    }
}
